/**
 * <p>js文件动态加载辅组类</p>

 * @module Ext-ux-cceaf-util
 * @class cceaf.util.ScriptLoaderMgr
 * @extends Object
 * @static
 * @since 1.0
 */
Ext.ns('cceaf.util');

Ext.define('cceaf.util.ScriptLoaderMgr', {
	//extjs cmd只能通过类查找，需定义空类
});

cceaf.util.ScriptLoaderMgr = ( function() {

		return {

			scriptTag: false,
			scripts : [],
			loadMask : null,

			showMask : function() {
				if (!this.loadMask) {
					this.loadMask = new Ext.LoadMask(Ext.getBody());
					this.loadMask.show();
				}
			},

			hideMask : function() {
				if (this.loadMask) {
					this.loadMask.hide();
					this.loadMask = null;
				}
			},

			processSuccess : function(response) {
				this.scripts[response.argument.url] = true;
				window.execScript ? window.execScript(response.responseText) : window.eval(response.responseText);
				if (response.argument.options.scripts.length == 0) {
					this.hideMask();
				}
				if ( typeof response.argument.callback == 'function') {
					response.argument.callback.call(response.argument.scope);
				}
			},

			processFailure : function(response) {
				this.hideMask();
				Ext.MessageBox.show({
					title : 'Application Error',
					msg : 'Script library could not be loaded.',
					closable : false,
					icon : Ext.MessageBox.ERROR,
					minWidth : 200
				});
				setTimeout(function() {
					Ext.MessageBox.hide();
				}, 3000);
			},

			/**
			 * 动态加载1个js文件
			 * @param {Object} cfg
			 * cfg: {
			 * 	url:null,
			 *  scope: 'window',
			 *  timeout: 10,
			 *  disableCaching: false,
			 *  callback: null,
			 * }
			 */
			loadOne : function(cfg, callback) {
				var callerScope, timeout = 30, disableCaching = false;
				var url = cfg;
				if ( typeof cfg == 'object') {// must be config object
					url = cfg.url;
					callback = callback || cfg.callback;
					callerScope = cfg.scope;

					if ( typeof cfg.timeout != 'undefined') {
						timeout = cfg.timeout;
					}
					if ( typeof cfg.disableCaching != 'undefined') {
						disableCaching = cfg.disableCaching;
					}
				}

				if (this.scripts[url]) {
					if ( typeof callback == 'function') {
						callback.call(callerScope || window);
					}
					return null;
				}

				this.showMask();

				if (this.scriptTag) {
					var script = document.createElement("script");
			        script.setAttribute("src", url);
			        script.setAttribute("type", "text/javascript");
			        document.getElementsByTagName("head")[0].appendChild(script);
			        
			        this.scripts[url] = true;
			        if ( typeof callback == 'function') {
						callback.call(callerScope || window);
					}
					
			        this.hideMask();
				} else {
					Ext.Ajax.request({
						url : url,
						cors: true, 
						success : this.processSuccess,
						failure : this.processFailure,
						scope : this,
						timeout : (timeout * 1000),
						disableCaching : disableCaching,
						argument : {
							'url' : url,
							'scope' : callerScope || window,
							'callback' : callback,
							'options' : cfg
						}
					});
				}
        		
			},

			/**
			 * 动态加载1组js文件
			 * @param {Object} objs
			 * {
			 * 		scripts: [],
			 * 		callback: null
			 * }
			 */
			load : function(objs) {
				var mgr = this;
				if (!Ext.isArray(objs.scripts)) {
					objs.scripts = [objs.scripts];
				}

				var callback = objs.callback;
				Ext.Array.each(objs.scripts, function(obj) {
					mgr.loadOne(obj, callback);
				});
			}
		};

	}());
